package com.lifeng.service.impl;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lifeng.common.util.PageResult;
import com.lifeng.entity.Book;
import com.lifeng.entity.Type;
import com.lifeng.mapper.BookMapper;
import com.lifeng.service.IBookService;
import com.lifeng.service.ITypeService;
import com.lifeng.vo.BookVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lifeng
 * @since 2025-01-13
 */
@Service
public class BookServiceImpl  extends ServiceImpl<BookMapper, Book> implements IBookService {
    @Autowired
    private ITypeService typeService;
    // 分页查询(不带条件)
    @Override
    public PageResult<Book> findByPage(Integer page, Integer pageSize) {
        Page<Book> page1 = this.page(new Page<>(page, pageSize));
        return new PageResult<>(page1.getRecords(),page1.getTotal());
    }

    //分页查询（带条件）
    @Override
    public PageResult<Book> search(Integer page, Integer pageSize, BookVO vo) {
        //封装查询条件
        QueryWrapper<Book> wrapper = new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(vo.getTitle()), "title", vo.getTitle());
        wrapper.like(StrUtil.isNotBlank(vo.getPublishHouse()), "publish_house", vo.getPublishHouse());
        wrapper.like(StrUtil.isNotBlank(vo.getAuthor()), "author", vo.getAuthor());
        wrapper.eq(vo.getTid() != null && vo.getTid()!= 0, "tid", vo.getTid());

        //根据wrapper包装好的条件进行分页查询
        Page<Book> page1 = this.page(new Page<>(page, pageSize), wrapper);


        page1.getRecords().forEach(st -> {
            Type type = typeService.getById(st.getTid());
            st.setTname(type.getTname());
        });


        return new PageResult<>(page1.getRecords(),page1.getTotal());
    }
}
